package com.ecloud.trend.utils;

import TrendDB.*;
import com.ecloud.common.tag.TagConstant;
import com.ecloud.common.exception.ServerException;
import com.ecloud.common.ResultEnum;
import org.springframework.stereotype.Component;

/**
 * @author Squbi
 */
@Component
public class TrendTemplate {

    // conn -------------------

    public int getTrendConnId() {
        return TrendConnFactory.getConn();
    }

    public boolean testConn() {
        try {
            int i = API.TestServer(getTrendConnId());
            if (i == 0) {
                return true;
            }
        } catch (DB_Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public void closeConn(int connId) {
        try {
            API.CloseConnection(connId);
        } catch (DB_Exception e) {
            e.printStackTrace();
            throw new ServerException(ResultEnum.DB_CLOSE_ERROR, e.GetErrMsg());
        }
    }

    // query ----------------------

    /**
     * 历史数据存储范围查询
     *
     * @param tag 标签 {@link TagConstant}
     * @return {@link HistoryStatus}
     * @author Squbi
     * @date 2021/6/28 10:38
     */
    public HistoryStatus queryHistStatus(String tag) {
        try {
            return API.QueryHistStatusByName(getTrendConnId(), tag);
        } catch (DB_Exception e) {
            e.printStackTrace();
            throw new ServerException(ResultEnum.DB_INNER_ERROR, e.GetErrMsg());
        }
    }

    /**
     * 最近更新值查询
     *
     * @param tags 标签 {@link TagConstant}
     * @return {@link DB_Value}
     * @author Squbi
     * @date 2021/6/28 10:41
     */
    public DB_Value[] queryLatestValue(String[] tags) {
        try {
            return API.GetLatestValueByName(getTrendConnId(), tags);
        } catch (DB_Exception e) {
            e.printStackTrace();
            throw new ServerException(ResultEnum.DB_INNER_ERROR, e.GetErrMsg());
        }
    }

    /**
     * 最近更新值查询
     *
     * @param tag 标签 {@link TagConstant}
     * @return {@link DB_Value}
     * @author Squbi
     * @date 2021/6/28 10:41
     */
    public DB_Value[] queryLatestValue(String tag) {
        String[] tags = {tag};
        return queryLatestValue(tags);
    }

    /**
     * 历史数据定位查询
     *
     * @param tag 标签 {@link TagConstant}
     * @param pos 历史定位 {@link HistoryPosition}
     * @return {@link HistoryPosition}
     * @author Squbi
     * @date 2021/6/28 10:47
     */
    public HistoryPosition queryHistPosition(String tag, HistoryPosition pos) {
        try {
            return API.QueryHistPositionByName(getTrendConnId(), tag, pos);
        } catch (DB_Exception e) {
            e.printStackTrace();
            throw new ServerException(ResultEnum.DB_INNER_ERROR, e.GetErrMsg());
        }
    }

    /**
     * 历史数据定位查询
     *
     * @param tag 标签 {@link TagConstant}
     * @return {@link HistoryPosition}
     * @author Squbi
     * @date 2021/6/28 11:00
     */
    public HistoryPosition queryHistPosition(String tag) {
        HistoryStatus status = queryHistStatus(tag);
        HistoryPosition position = new HistoryPosition();
        position.start = status.start;
        position.end = status.end;
        return queryHistPosition(tag, position);
    }

    /**
     * 历史原始数据查询
     *
     * @param tag 标签 {@link TagConstant}
     * @param pos {@link HistoryPosition}
     * @return {@link DB_Value}
     * @author Squbi
     * @date 2021/6/28 11:16
     */
    public DB_Value[] queryTrend(String tag, HistoryPosition pos) {
        try {
            return API.RetrieveTrendByName(getTrendConnId(), tag, pos);
        } catch (DB_Exception e) {
            e.printStackTrace();
            throw new ServerException(ResultEnum.DB_INNER_ERROR, e.GetErrMsg());
        }
    }

    /**
     * 历史原始数据全部查询
     *
     * @param tag 标签 {@link TagConstant}
     * @return {@link DB_Value}
     * @author Squbi
     * @date 2021/6/28 11:16
     */
    public DB_Value[] queryTrendAll(String tag) {
        HistoryPosition position = queryHistPosition(tag);
        return queryTrend(tag, position);
    }
}
